import asyncio
import websockets as ws
from websockets import ConnectionClosed


async def hello():
    uri = "ws://localhost:8899"
    while True:
        try:
            async with ws.connect(uri) as websocket:
                await websocket.send("start")
                while True:
                    try:
                        await websocket.recv()
                    except ConnectionClosed as e:
                        print(e.code)
                        if e.code == 1006:
                            print("restart")
                            await asyncio.sleep(2)
                            break
        except ConnectionRefusedError as e:
            print(e)
            await asyncio.sleep(2)


asyncio.get_event_loop().run_until_complete(hello())
